/* Orbas:
 *     A open source CORBA Specification implementation from Huihoo.
 *
 * Copyright 2002-2003 Huihoo.org, Inc. All Right Reserved.
 *
 * This software is licensed under LGPL license.
 * See terms of license at gnu.org.
 *
 * For more information, visit:
 *
 * http://www.huihoo.org/orbas
 */


package org.huihoo.orbas.orb.thread;

/**
 * <p>Description: </p>
 * Incoming request handlers registry.
 * <p>Copyright (c) 2002,2003</p>
 * <p>Company: <a href="http://www.huihoo.org/">huihoo.org</a></p>
 * @author <a href="http://www.huihoo.org/~mep">mep(mep@huihoo.com)</a>
 * @see <a href="http://www.huihoo.org/orbas">http://www.huihoo.org/orbas</a>
 * @version 1.0
 */


import com.cognition.util.j2me.ApplicationProperties;
import com.cognition.util.j2me.Logger;
import com.cognition.util.j2me.SystemLogger;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import org.huihoo.orbas.orb.ORB;
import org.huihoo.orbas.orb.ServerWorker;
import org.huihoo.orbas.orb.giop.GiopServerWorker;
import org.huihoo.orbas.orb.protocols.Transport;

public class ServerWorkerRegistry implements java.lang.Runnable {
    private static Logger logger = SystemLogger.getLogger(SystemLogger.LOGGER_CONSOLE);
    public static final String LOG_TAG = "ServerWorkerRegistry";

    ORB orb;

    /**
     * @clientCardinality 1 
     */
    RequestQueue inputQueue;
    boolean flag;
    Vector transports;
    ServerWorkerRunner runner;
    
    public ServerWorkerRegistry(ORB orb) {
        this.orb = orb;
        inputQueue = orb.getRequestQueue();

        flag = true;
        transports = new Vector();
    }
    
    public void run() {

        while(flag) {
            Transport transport;
            transport = (Transport)inputQueue.removeFromQueue();

            // lower layer has received inbound connection - now we need
            // to process the response.
            
            if( transport == null ) {
                logger.error(LOG_TAG, "got null transport");
                continue;
            }
            if (ApplicationProperties.SERVERWORKER_REGISTRY_MULTITHREAD){
                // let the default server worker handle requests until the
                // connection with the client closes.
                java.lang.Thread t = new java.lang.Thread(orb.getDefaultServerWorker(transport));
                t.start();
            } else {
                transports.addElement(orb.getDefaultServerWorker(transport));
                runner.notifyAll();
            }
        }
    }
    
    public void shutdown() {
        flag = false;
    }

    private class ServerWorkerRunner extends Thread{
        public void run(){
            while(true){
                if (transports.size() == 0)
                    try{
                        runner.wait();
                    } catch (InterruptedException ie){
                        // do nothing.
                    }

                for(Enumeration en = transports.elements(); en.hasMoreElements();){
                    ServerWorker worker = (ServerWorker) en.nextElement();
                    Transport tpt = worker.getTransport();
                    try {
                        if (tpt.getInputStream().available() > 0) {
                            worker.runNonContinuous();
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }

        }

    }
}